package com.docwei.thansgank.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by tobo on 17/7/25.
 */

public class NoScrollViewPager extends ViewPager {
    public NoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollViewPager(Context context) {
        super(context);
    }
    private int mDownX;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        requestDisallowInterceptTouchEvent(true);
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mDownX = (int) ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX= (int) ev.getX();
                int dx= moveX-mDownX;
                if(Math.abs(dx)>0){
                    //左右滑动的事件,申请拦截
                    requestDisallowInterceptTouchEvent(true);
                }
                mDownX=moveX;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /*@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = (int) ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) ev.getX();
                int dx = moveX - mDownX;
                mDownX = moveX;
                if (Math.abs(dx) > 0) {
                    //申请拦截这个事件,不让左右的事件传过来
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }*/
}

